# -*- coding: utf-8 -*-
import types

startProbability = {}
#for c in greek_ch:
#    startProbability[c] = 0

hiddenStateTransitions = {}
#for c1 in greek_ch:
#    for c2 in greek_ch:
#        hiddenStateTransitions[(c1,c2)]=0
        
hiddenStateToObservation = {}
#for c1 in greek_ch:
#    for c2 in greeklish_ch:
#        hiddenStateToObservation[(c1,c2)]=0
         
def addWordGreek(word_gr, count=1):
    assert (type(word_gr)==types.UnicodeType)
    #print 'adding: ', word_gr.lower().encode('utf-8'), word_grkl.encode('utf-8'), count
    try:
        start_probability(word_gr, count)
        hidden_transition(word_gr, count)
    except KeyError:
        print 'KeyError', word_gr.encode('utf-8')
        raise 'KeyError'
          
        
def addWordGreeklish(word_gr, word_grkl, count=1):
    assert (type(word_gr)==types.UnicodeType)
    try:
        assert(len(word_gr)==len(word_grkl))
    except (AssertionError):
        print 'different length'
    #print 'adding: ', word_gr.lower().encode('utf-8'), word_grkl.encode('utf-8'), count
    try:
        hidden_to_observation(word_gr, word_grkl, count)
    except KeyError:
        print 'KeyError', word_gr.encode('utf-8')
        raise 'KeyError'

        
def hidden_to_observation(word_gr, word_grkl, count):
    for i in range(0, len(word_gr)):
        #if not hiddenStateToObservation[(word_gr[i], word_grkl[i])] : hiddenStateToObservation[(word_gr[i], word_grkl[i])] = 0
        hiddenStateToObservation[(word_gr[i], word_grkl[i])] = hiddenStateToObservation.get((word_gr[i], word_grkl[i]), 0 ) + count    
        
        
def hidden_transition(word_gr, count):
    prev = None
    for c in word_gr:
        if(prev):
             #if not hiddenStateTransitions[(prev,c)]: hiddenStateTransitions[(prev,c)] = 0
             hiddenStateTransitions[(prev,c)] = hiddenStateTransitions.get((prev,c),0) + count
        prev = c

def start_probability(word_gr, count):    
    start = word_gr[0]
    #if not startProbability[start]: startProbability[start] =0
    startProbability[start] =     startProbability.get(start, 0 ) + count
    
import util
def train(file, function):
    print 'Reading and refining words...'
    words = util.refineWords(util.read_words(file))
    print 'Adding words...'
    for word in words.samples():
        word2 = function(word.lower())
        #print 'adding: ', word.lower().encode('utf-8'), word2.encode('utf-8'), words.count(word)
        addWordGreek(word.lower(), words.count(word))
        addWordGreeklish(word.lower(), word2, words.count(word))
    return words.N()

    

    


def test():
    words = [(u'ορέστης', 'orestis'),
             (u'άρης', 'aris'),
             (u'τάκης', 'takis'),
             ]
    for (word_gr, word_grkl) in words:
        addWord(word_gr, word_grkl)
    
    res1 =  [(char,cnt) for (char, cnt) in startProbability.items() if cnt>0]
    res2 = [(char[0], char[1],cnt) for char, cnt in hiddenStateTransitions.items() if cnt>0]
    res3 = [(char[0], char[1],cnt) for char, cnt in hiddenStateToObservation.items() if cnt>0]
    
    print 'startProbability'
    for (c, cnt) in res1:
        print "%s - %s" % (c.encode('utf-8'), cnt)
    print 'hidden state transitions'
    for (c1, c2, cnt) in res2:
        print "(%s,%s) - %s" % (c1.encode('utf-8'),c2.encode('utf-8'), cnt)        
    print 'hidden state to observations'
    for (c1, c2, cnt) in res3:
        print "(%s,%s) - %s" % (c1.encode('utf-8'),c2.encode('utf-8'), cnt)        
    
    #print hiddenStateToObservation.values()
    
if __name__ == '__main__':
    test()
    
        

